package com.rui.study.test.ehcache.service.impl;

import com.rui.study.test.ehcache.entity.User;
import com.rui.study.test.ehcache.mapper.UserMapper;
import com.rui.study.test.ehcache.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @program: study
 * @description:
 * @author: Yaowr
 * @create: 2019-03-07 17:26
 **/
@Service
public class UserServiceImpl implements UserService {

    private static final String CACHE_KEY = "'USER-DEFAULT-KEY'";
    private static final String CACHE_NAME = "User";

    @Autowired
    private UserMapper userMapper;

    @CacheEvict(cacheNames = CACHE_NAME, key = CACHE_KEY)
    @Override
    public void createUser(User user) {
        userMapper.insertUser(user);
    }

    @Cacheable(cacheNames = CACHE_NAME, key = "'USER-' + #unionId")
    @Override
    public User getUser(int unionId) {
        return userMapper.selectUser(unionId);
    }

    @CacheEvict(cacheNames = CACHE_NAME, key = "'USER-' + #user.unionId")
    @Override
    public void modifyUser(User user) {
        userMapper.updateUser(user);

    }

    @CacheEvict(cacheNames = CACHE_NAME, key = "'USER-' + #unionId")
    @Override
    public void deleteUser(int unionId) {
        userMapper.deleteUser(unionId);
    }
}
